package netty;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Author: zhangrongqing
 * @Date: 2020/1/7 16:34
 */
public class ByteBufferUtil {
    public static void readByteBuffer(SocketChannel socketChannel,ByteBuffer byteBuffer){
        while (true){
            try {
                byteBuffer.clear();
                int read = socketChannel.read(byteBuffer);
                if(read <= 0){
                    break;
                }
//                while(byteBuffer.hasRemaining()){
//                    System.out.println(new String(byteBuffer.get()));
//                }
                byteBuffer.flip();
                byte[] bytes = new byte[read];
                byteBuffer.get(bytes,0,byteBuffer.limit());
                System.out.println(new String(bytes));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
